package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.mapper.OrderDiagMapper;
import com.itheima.service.OrderDiagService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = OrderDiagService.class)
@Transactional
public class OrderDiagServiceImpl implements OrderDiagService {
    @Autowired
    private OrderDiagMapper orderDiagMapper;

    //根据日期查询预约人数
    public List<Integer> findOrderCountByDates(List<String> dates) {
        List<Integer> orderCount = new ArrayList<>();
        for (String date : dates) {
            orderCount.add(orderDiagMapper.findOrderCountByDates(date));
        }
        return orderCount;
    }

    //根据日期查询到诊人数
    public List<Integer> findDiagCountByDates(List<String> dates) {
        List<Integer> diagCount = new ArrayList<>();
        for (String date : dates) {
            diagCount.add(orderDiagMapper.findDiagCountByDates(date));
        }
        return diagCount;
    }

    //查询套餐收入及总和
//    public List<Map<String, Object>> findSetmealIncome() {
//        List<Map<String, Object>> setmealIncome = orderDiagMapper.findSetmealIncome();//name value
//        Map<String, Object> map1 = new HashMap<>();
//        Map<String, Object> map2 = new HashMap<>();
//        double value = 0;
//        String name = "总收入";
//        for (Map<String, Object> map : setmealIncome) {
//            double value1 = (double) map.get("value");
//            value = value + value1;
//        }
//        map1.put(name, value);
//        map2.put("name", name);
//        map2.put("value", value);
//        setmealIncome.add(map2);
//        return setmealIncome;
//    }

    //根据日期间隔查询预约人数和到诊人数
    public Map<String, Object> findCount(String value1) throws Exception {
        //存储查询的预约人数和到诊人数
        Map map = new HashMap();
        //根据逗号分隔前端传来的日期"2020-09-03,2020-10-03"
        String[] date = value1.split(",");
        //拿到第一个日期
        String startDate = date[0];
        //拿到第二个日期
        String endDate = date[1];
        //生成日期间的日期集合
        List<String> dates = DateUtils.getMonthDayBetween(startDate, endDate, "yyyy-MM-dd");
        map.put("dates", dates);
        // 根据日期范围查询预约人数
        Map map1 = new HashMap();
        map1.put("startDate", startDate);
        map1.put("endDate", endDate);
        //根据时间查询预约人数
        List<Integer> orderCount = orderDiagMapper.findOrderCount(map1);
        //根据日期范围查询到诊人数
        List<Integer> diagCount = orderDiagMapper.findDiagCount(map1);
        map.put("orderCount", orderCount);
        map.put("diagCount", diagCount);
        return map;
    }

    //根据日期查询套餐收入
    public Map<String, Object> getSetmealReportMoneyDate(String value1) {
        //返回给前端数据
        Map map = new HashMap();
        //根据逗号分隔前端传来的日期"2020-09-03,2020-10-03"
        String[] date = value1.split(",");
        //拿到第一个日期
        String startDate = date[0];
        //拿到第二个日期
        String endDate = date[1];
        Map map1 = new HashMap();
        map1.put("startDate", startDate);
        map1.put("endDate", endDate);
        //用于存储套餐名
        List<String> setmealNames = new ArrayList<>();
        List<Map<String, Object>> setIncome = orderDiagMapper.getSetmealReportMoneyDate(map1);
        //存储计算出的总收入及金额
        Map map4 = new HashMap();
        String name = "总收入";
        double value = 0;
        //遍历数据库查询的集合取出对应的套餐名称存入setmealNames集合
        for (Map<String, Object> map2 : setIncome) {
            String name1 = (String) map2.get("name");
            setmealNames.add(name1);
            double value2 = (double) map2.get("value");
            value = value + value2;
        }
        map4.put("name", name);
        map4.put("value", value);
        //用于拼接成前端需要的数据格式方便取值({"name":"妄想症","value":5000.0},{"name":"总收入","value":11100.0})
        setIncome.add(map4);
        map.put("setmealNames", setmealNames);
        map.put("setmealIncome", setIncome);
        return map;
    }

//    //用户没有输入查询日期时直接加载生成一年的饼图
//    public List<Map<String, Object>> getSetmealReportMoney(List<String> months) {
//        List<Map<String, Object>> setmealIncome = new ArrayList<>();
//        for (int i = 0; i < months.size(); i++) {
//            List<Map<String, Object>> monthList = orderDiagMapper.getSetmealReportMoney(months.get(i));
//            Map map = new HashMap();
//            for (int j = 0; j < monthList.size(); j++) {
//                Map<String, Object> monthMap = monthList.get(j);
//                Set<Map.Entry<String, Object>> entries = monthMap.entrySet();
//                for (Map.Entry<String, Object> entry : entries) {
//                    map.put(entry.getKey(), entry.getValue());
//                }
//            }
//            setmealIncome.add(map);
//        }
//        return setmealIncome;
//    }

}
